package com.faxsun.api.wrapper.category;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.catalog.domain.Category;
import org.broadleafcommerce.core.catalog.domain.Product;
import org.broadleafcommerce.core.search.domain.ProductSearchCriteria;
import org.broadleafcommerce.core.search.domain.ProductSearchResult;
import org.broadleafcommerce.core.search.service.SearchService;
import org.broadleafcommerce.core.web.api.BroadleafWebServicesException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.faxsun.api.wrapper.product.ProductWrapper;




@Component("fsCategoryWrapper")
@Scope("prototype")
@XmlRootElement(name = "fsCategory")
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlType(name="fsCategoryResource")
public class CategoryWrapper extends org.broadleafcommerce.core.web.api.wrapper.CategoryWrapper {

	@Override
	public void wrapDetails(Category category, HttpServletRequest request) {
		super.wrapDetails(category, request);
		this.products =null;
		
		Integer productLimit = (Integer) request.getAttribute("productLimit");
        Integer productOffset = (Integer) request.getAttribute("productOffset");
		
		if (productLimit != null && productOffset != null) {
            SearchService searchService = getSearchService();
            ProductSearchCriteria searchCriteria = new ProductSearchCriteria();
            searchCriteria.setPage(productOffset);
            searchCriteria.setPageSize(productLimit);
            searchCriteria.setFilterCriteria(new HashMap<String, String[]>());
            try {
                ProductSearchResult result = searchService.findExplicitProductsByCategory(category, searchCriteria);
                List<Product> productList = result.getProducts();
                if (productList != null && !productList.isEmpty()) {
                    if (products == null) {
                        products = new ArrayList<org.broadleafcommerce.core.web.api.wrapper.ProductWrapper>();
                    }

                    for (Product p : productList) {
                    	ProductWrapper productSummaryWrapper = (ProductWrapper) context.getBean("fsProductWrapper");
                        productSummaryWrapper.wrapSummary(p, request);
                        products.add(productSummaryWrapper);
                    }
                }
            } catch (ServiceException e) {
                throw BroadleafWebServicesException.build(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), null, null, e);
            }
        }
	}

}
